#include "volumetool.h"
#include "ui_volumetool.h"
#include<QGraphicsDropShadowEffect>
#include<QPainter>
VolumeTool::VolumeTool(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::VolumeTool),
    isMuted(false)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Popup|Qt::FramelessWindowHint|Qt::NoDropShadowWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    QGraphicsDropShadowEffect*shadowEffect=new QGraphicsDropShadowEffect(this);
    shadowEffect->setColor("#646464");
    shadowEffect->setBlurRadius(10);
    shadowEffect->setOffset(0,0);
    this->setGraphicsEffect(shadowEffect);
    //设置静音按钮图标
    ui->silenceBtn->setIcon(QIcon(":/volumn3.png"));
    ui->volumeRadio->setText("20%");
    QRect rect=ui->outline->geometry();
    //进度条高度调整为原高度的20%，25是进度条离父元素的距离,也就是ouline的y坐标
    ui->outline->setGeometry(rect.x(),rect.height()-rect.height()*0.2+ui->outline->y(),rect.width(),rect.height()*0.2);
    //移动绿圆点的坐标，调整后的ouline左上角y坐标减去圆点半径
    ui->slideBtn->move(ui->slideBtn->x(),ui->outline->y()-ui->slideBtn->height()/2);
    connect(ui->silenceBtn,&QPushButton::clicked,this,&VolumeTool::onslicenBtnClicked);
    ui->volumeBox->installEventFilter(this);
}

VolumeTool::~VolumeTool()
{
    delete ui;
}

void VolumeTool::paintEvent(QPaintEvent *event)
{
    (void)event;
    //创建画家
   QPainter painter(this);
   //设置画笔
   painter.setPen(Qt::NoPen);
   //设置画刷
   painter.setBrush(QBrush(Qt::white));
   //绘制三角形
   QPolygon polygon;
   QPoint a(ui->volumeWidget->x(),ui->volumeWidget->y()+ui->volumeWidget->height());
   QPoint b(ui->volumeWidget->x()+ui->volumeWidget->width(),ui->volumeWidget->y()+ui->volumeWidget->height());
   QPoint c(ui->volumeWidget->x()+ui->volumeWidget->width()/2,ui->volumeWidget->y()+ui->volumeWidget->height()+30);
   polygon.append(a);
   polygon.append(b);
   polygon.append(c);
   painter.drawPolygon(polygon);
}

void VolumeTool::onslicenBtnClicked()
{
    isMuted=!isMuted;
    if(isMuted)
    {
        //静音
        ui->silenceBtn->setIcon(QIcon(":/silent1.png"));
    }
    else
    {
        //非静音
        ui->silenceBtn->setIcon(QIcon(":/volumn3.png"));
    }

    //发射信号，通知QQMusic处理
    emit setMusicMuted(isMuted);
}

bool VolumeTool::eventFilter(QObject *watched, QEvent *event)
{
    if(ui->volumeBox==watched)
    {
        if(event->type()==QEvent::MouseButtonPress)
        {
            //鼠标按下事件
            calcVolume();
        }
        else if(event->type()==QEvent::MouseButtonRelease)
        {
            //鼠标释放事件
            emit setMusicVolume(volumeRadio);
        }
        else if(event->type()==QEvent::MouseMove)
        {
            //鼠标移动事件
            calcVolume();
            emit setMusicVolume(volumeRadio);
        }
        return true;
    }
    return QObject::eventFilter(watched,event);
}

void VolumeTool::calcVolume()
{
    //获取鼠标点击时的Y坐标
    int height=ui->volumeBox->mapFromGlobal(QCursor().pos()).y();
    //验证height合法性
    height=height<25?25:height;
    height=height>225?225:height;
    //更新outline
    ui->outline->setGeometry(ui->outline->x(),height,ui->outline->width(),225-height);
    //更新silideBtn
    ui->slideBtn->move(ui->slideBtn->x(),ui->outline->y()-ui->slideBtn->height()/2);
    //计算音量大小
    volumeRadio=(int)(ui->outline->height()/(float)225*100);
    //更新音量百分比
    ui->volumeRadio->setText(QString::number(volumeRadio)+"%");
}
